/*
 *   Copyright 2011-2012 Hai Bison
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package group.pals.mailgate.beans;

import group.pals.ciphers.HashCalc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
 *
 * @author Hai Bison
 */
public class ItemGroupEmailMessages extends ArrayList<ItemEmailMessage> {

    /**
     * Total e-mails.
     */
    private int count;

    /**
     * Gets the newest e-mail message.
     * @return the newest e-mail message, or <code>null</code> if this list is empty.
     */
    public ItemEmailMessage getNewestMessage() {
        if (isEmpty()) {
            return null;
        }

        Date date = new Date(0);
        ItemEmailMessage newestMessage = null;
        for (ItemEmailMessage message : this) {
            if (message.getReceivedDate().after(date)) {
                newestMessage = message;
            }
        }
        return newestMessage;
    }

    /**
     * @return the sender of newest e-mail, or <code>null</code> if this list is empty.
     */
    @Override
    public String toString() {
        ItemEmailMessage newestMessage = getNewestMessage();
        if (newestMessage != null) {
            return newestMessage.getSubject();
        }
        return null;
    }

    /**
     * Checks if this list has same messages with <code>other</code>.
     * @param other
     * @return 
     */
    public boolean hasSameMessages(ItemGroupEmailMessages other) {
        if (isEmpty() || other.isEmpty()) {
            return false;
        }

        if (size() == other.size()
                && getNewestMessage().getSender().equals(other.getNewestMessage().getSender())) {
            HashCalc hc = new HashCalc(HashCalc.HashAlgorithm.haCRC32);

            long[] hashes = new long[size()];
            for (int i = 0; i < size(); i++) {
                hc.update(get(i).getID().getBytes());
                hashes[i] = hc.finalCRC32Hash();
                hc.reset();
            }

            long[] newHashes = new long[other.size()];
            for (int i = 0; i < size(); i++) {
                hc.update(get(i).getID().getBytes());
                newHashes[i] = hc.finalCRC32Hash();
                hc.reset();
            }

            return Arrays.equals(hashes, newHashes);
        }

        return false;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    /**
     * @param count the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }
}
